/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HakuIkkuna.java
 *
 * Created on 6.4.2009, 10:25:39
 */
package kayttoliittyma;

import java.util.ArrayList;
import java.util.Vector;
import opintopaivakirja.*;

/**
 *
 * @author Erkkinuuttinen
 */
public class HakuIkkuna extends javax.swing.JDialog {

    /** Creates new form HakuIkkuna
     * @param parent
     * @param modal
     * @param kayttaja = 
     */
    public HakuIkkuna(java.awt.Frame parent, boolean modal, Kayttaja kayttaja) {
        super(parent, modal);

        this.kayttaja = kayttaja;

        // oletus
        checkPoistetut = false;
        checkValmiit = false;
        String tyhjatagi = "";

        ArrayList<Tagi> tagit = new Tagi().selectKayttajanTagit(kayttaja.getId());

        initComponents();

        // tagit valikko
        Vector<String> ktagit = new Vector<String>();
        for (Tagi tag : tagit) {
            ktagit.add(tag.getSana());
        }
        ktagit.add(tyhjatagi);
        jComboBoxTagit.setModel(new javax.swing.DefaultComboBoxModel(ktagit));
        jComboBoxTagit.updateUI();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldHakuKentta = new javax.swing.JTextField();
        jCheckBoxPoistetut = new javax.swing.JCheckBox();
        jCheckBoxValmiit = new javax.swing.JCheckBox();
        jComboBoxTagit = new javax.swing.JComboBox();
        jButtonHae = new javax.swing.JButton();
        jPanelTulokset = new javax.swing.JPanel();
        jLabelTulos = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTextFieldHakuKentta.setName("jTextFieldHakuKentta"); // NOI18N

        jCheckBoxPoistetut.setText("Poistetut");
        jCheckBoxPoistetut.setName("jCheckBoxPoistetut"); // NOI18N
        jCheckBoxPoistetut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxPoistetutActionPerformed(evt);
            }
        });

        jCheckBoxValmiit.setText("Valmiit");
        jCheckBoxValmiit.setName("jCheckBoxValmiit"); // NOI18N
        jCheckBoxValmiit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxValmiitActionPerformed(evt);
            }
        });

        jComboBoxTagit.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxTagit.setName("jComboBoxTagit"); // NOI18N

        jButtonHae.setText("Hae");
        jButtonHae.setName("jButtonHae"); // NOI18N
        jButtonHae.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonHaeActionPerformed(evt);
            }
        });

        jPanelTulokset.setName("jPanelTulokset"); // NOI18N

        jLabelTulos.setName("jLabelTulos"); // NOI18N

        javax.swing.GroupLayout jPanelTuloksetLayout = new javax.swing.GroupLayout(jPanelTulokset);
        jPanelTulokset.setLayout(jPanelTuloksetLayout);
        jPanelTuloksetLayout.setHorizontalGroup(
            jPanelTuloksetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTuloksetLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTulos, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelTuloksetLayout.setVerticalGroup(
            jPanelTuloksetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTuloksetLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTulos)
                .addContainerGap(46, Short.MAX_VALUE))
        );

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel1.setText("Tulokset:");
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(346, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBoxPoistetut, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .addGap(200, 200, 200)
                        .addComponent(jButtonHae, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jCheckBoxValmiit)
                    .addComponent(jComboBoxTagit, 0, 380, Short.MAX_VALUE)
                    .addComponent(jTextFieldHakuKentta, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelTulokset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextFieldHakuKentta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jComboBoxTagit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBoxValmiit)
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBoxPoistetut)
                    .addComponent(jButtonHae))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelTulokset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonHaeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHaeActionPerformed

        String hakulause = jTextFieldHakuKentta.getText().trim();

        String tagilause = "";
        Object[] objt = jComboBoxTagit.getSelectedObjects();
        for(int i = 0; i < objt.length; ++i){
            tagilause += " " + objt[i].toString();
        }

        ArrayList<Projekti> tulokset = new ArrayList<Projekti>();
        tulokset = kayttaja.haeTiedot(hakulause, tagilause, checkValmiit, checkPoistetut);
        
        String ruma = "";
        for (Projekti projekti : tulokset) {
            ruma += projekti.getNimi() +  "--"; //+ " (" + projekti.getPvm_paivitetty() + ") /// ";
        }
        jLabelTulos.setText(ruma);






        // TODO Hae

    }//GEN-LAST:event_jButtonHaeActionPerformed

    private void jCheckBoxValmiitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxValmiitActionPerformed

        if(jCheckBoxValmiit.isSelected()){
            checkValmiit = false;
            System.out.println("valmiit asetettiin to false");
        } else {
            checkValmiit = true;
        }
    }//GEN-LAST:event_jCheckBoxValmiitActionPerformed

    private void jCheckBoxPoistetutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxPoistetutActionPerformed
        if(jCheckBoxPoistetut.isSelected()){
            checkPoistetut = false;
            System.out.println("POISTETUT asetettiin to false");
        } else {
            checkPoistetut = true;
        }
    }//GEN-LAST:event_jCheckBoxPoistetutActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                HakuIkkuna dialog = new HakuIkkuna(new javax.swing.JFrame(), true, );
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonHae;
    private javax.swing.JCheckBox jCheckBoxPoistetut;
    private javax.swing.JCheckBox jCheckBoxValmiit;
    private javax.swing.JComboBox jComboBoxTagit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelTulos;
    private javax.swing.JPanel jPanelTulokset;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextFieldHakuKentta;
    // End of variables declaration//GEN-END:variables
    Kayttaja kayttaja;
    boolean checkValmiit;
    boolean checkPoistetut;
}
